package com.gitee.huxing.course.conc.conc03.collection;

import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Description:
 * @ProjectName: JavaCourse
 * @Package: com.gitee.huxing.course.conc.conc03.collection
 * @ClassName: CopyOnWriteArrayListDemo2
 * @Author: huxingmale
 * @DateTime: 2021-08-25 22:52
 */
public class CopyOnWriteArrayListDemo2 {

    private final static CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList();

    public static void main(String[] args) {
        test();
    }

    public static void test(){
        for(int i = 0; i<10000; i++){
            list.add("string" + i);
        }
        new Thread(() -> {
            while (true) {
                if (list.size() > 0) {    // todo ： 下一个get操作执行时，size可能已经是0了
                    String content = list.get(list.size() - 1);
                }else {
                    break;
                }
            }
        }).start();

        new Thread(() -> {
            while (true) {
                if(list.size() <= 0){
                    break;
                }
                list.remove(0);
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
